<?php

function reequipment_userapi_getitems($args)
{
    extract($args);

    /* Fields in table.
    item_id
	part_number
	invoice_number
	model_id
	equipment_id                                                                                                       
	company_id                                                                                                           
	last_ordered                   
	man_part_number
	cost
   */

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['reitems'];
    $join_equipment = $xartable['reequipment'];
    $join_model = $xartable['remodels'];
    $join_area = $xartable['reareas'];
    $join_company = $xartable['rel_eq'];

    $query = "SELECT DISTINCT i.item_id,
					i.part_number,
					i.invoice_number,
					i.description,
					i.model_id,
					i.equipment_id,                                                                                                       
					i.company_id,                                                                                                         
					i.last_ordered,                   
					i.man_part_number,
					i.cost,
					e.serial_number,
					e.cycles,
					e.machine_name,
					m.model_name,
					a.area_name,
					r.company_name"
    		. " FROM $table i"
    		
    		. " LEFT OUTER JOIN $join_model m"
    		. " ON i.model_id = m.model_id"
    		
    		. " LEFT OUTER JOIN $join_area a"
    		. " ON m.area_id = a.area_id"
    		
    		. " LEFT OUTER JOIN $join_equipment e"
    		. " ON i.equipment_id = e.equipment_id"    	
    		
    		. " LEFT OUTER JOIN $join_company r"
    		. " ON i.company_id = r.company_id";       		

    $where = array();
    $bind = array();
    
    if (!empty($item_id)) {
        $where[] = 'i.item_id = ?';
        $bind[] = (int)$item_id;
    }
    
    if (!empty($part_number)) {
        $where[] = 'i.part_number = ?';
        $bind[] = (string)$part_number;
    }    
    
    if (!empty($invoice_number)) {
        $where[] = 'i.invoice_number = ?';
        $bind[] = (string)$invoice_number;
    } 
    
    if (!empty($model_id)) {
        $where[] = 'i.model_id = ?';
        $bind[] = (int)$model_id;
    }    

    if (!empty($equipment_id)) {
        $where[] = 'i.equipment_id = ?';
        $bind[] = (int)$equipment_id;
    }  
    
    if (!empty($company_id)) {
        $where[] = 'i.company_id = ?';
        $bind[] = (int)$company_id;
    }      
    
    // Figure out the dates when we need it.

    if (!empty($man_part_number)) {
        $where[] = 'i.man_part_number = ?';
        $bind[] = (string)$man_part_number;
    }    

    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }

    // Not really necessary
    $query .= " ORDER BY i.last_ordered DESC";

    $result =& $dbconn->SelectLimit($query, $numitems, $startnum-1, $bind);
    if (!$result) return;

    $tags = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($item_id, $part_number, $invoice_number, $description, $model_id, $equipment_id, $company_id, $last_ordered, $man_part_number, $cost, $serial_number, $cycles, $machine_name, $model_name, $area_name, $company_name) = $result->fields;

        $tags[] = array(
        		'item_id'			=> $item_id,
        		'part_number'		=> xarVarPrepForDisplay($part_number),
        		'invoice_number'	=> xarVarPrepForDisplay($invoice_number),
        		'description'		=> xarVarPrepForDisplay($description),
                'model_id'      	=> $model_id,
                'equipment_id'      => $equipment_id,
                'company_id'		=> $company_id,
                'last_ordered' 		=> xarLocaleGetFormattedDate('long', $last_ordered),
                'man_part_number'	=> xarVarPrepForDisplay($man_part_number),
                'cost' 				=> round($cost, 2),
                'serial_number'    	=> xarVarPrepForDisplay($serial_number),
                'cycles'			=> $cycles,
                'machine_name'    	=> xarVarPrepForDisplay($machine_name),
                'model_name'    	=> xarVarPrepForDisplay($model_name),
                'area_name'    		=> xarVarPrepForDisplay($area_name), 
                'company_name'    	=> xarVarPrepForDisplay($company_name)              
        );
    }

    $result->Close();
    return $tags;
}
?>